#include <memory>
#include <string>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

class SubscriberNode : public rclcpp::Node
{
   public:
    SubscriberNode() : Node("subscriber_node")
    {
        // 声明参数
        this->declare_parameter("node_name", "subscriber_node");

        // 获取参数
        std::string node_name = this->get_parameter("node_name").as_string();

        // 创建订阅者
        subscription_ = this->create_subscription<std_msgs::msg::String>(
            "topic", 10, [this](const std_msgs::msg::String::SharedPtr msg) {
                RCLCPP_INFO(this->get_logger(), "Received: '%s'", msg->data.c_str());
            });

        RCLCPP_INFO(this->get_logger(), "Subscriber node '%s' started", node_name.c_str());
    }

   private:
    rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};

int main(int argc, char *argv[])
{
    rclcpp::init(argc, argv);
    rclcpp::spin(std::make_shared<SubscriberNode>());
    rclcpp::shutdown();
    return 0;
}
